package com.hxd.otc.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.dialog.widget.internal.InternalBasePopup;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.base.OnItemClickListener;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.StringUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/9/15 0015.
 * description ${钱包菜单}
 */
public class OrderPaymentMethodPopupWindow extends BasePopupWindow<OrderPaymentMethodPopupWindow> implements OnItemClickListener {
    public static final int BANK_TYPE_WRITE = -1;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private DealOrderPaymentMethodAdapter mAdapter;
    private ArrayList<BankCardBean> mDatas;
    private BankCardBean mSelItem;
    private OnSelPayMethodListener mOnSelPayMethodListener;
    private int mWidth;

    public OrderPaymentMethodPopupWindow(Context context) {
        super(context);
    }

    @Override
    public View onCreatePopupView() {
        return View.inflate(mContext, getLayoutId(), null);
    }

    protected int getLayoutId() {
        return R.layout.popup_advert_fiat;
    }

    @Override
    public void onViewCreated(View view) {
        super.onViewCreated(view);
        ButterKnife.bind(this, view);
        mRecyclerView.setLayoutManager(getLayoutManager());
        mAdapter = createAdapter();
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @NonNull
    protected DealOrderPaymentMethodAdapter createAdapter() {
        return new DealOrderPaymentMethodAdapter(getContext());
    }

    public OrderPaymentMethodPopupWindow setData(ArrayList<BankCardBean> datas) {
        mDatas = datas;
        return this;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    public OrderPaymentMethodPopupWindow setSelItem(BankCardBean selItem) {
        mSelItem = selItem;
        return this;
    }

    @Override
    public void setUiBeforShow() {
        mAdapter.setSelPosition(mSelItem);
        mAdapter.addAll(true, mDatas);
        ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
        layoutParams.width = mWidth;
        if (mDatas != null && mDatas.size() > 4) {
            layoutParams.height = DataUtils.dp2px(getContext(), 250);
        } else {
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        mRecyclerView.setLayoutParams(layoutParams);
    }

    @Override
    public void onItemClick(int position) {
        if (mOnSelPayMethodListener != null) {
            mOnSelPayMethodListener.onSelPayMethod(mAdapter.getItem(position));
            dismiss();
        }
    }

    public InternalBasePopup setWidth(int width) {
        mWidth = width;
        return this;
    }


    static class DealOrderPaymentMethodAdapter extends BaseAdapter<FiatHolder, BankCardBean> {
        protected BankCardBean mSelItem;


        DealOrderPaymentMethodAdapter(Context context) {
            super(context);
        }

        @Override
        protected void onBindHolder(@NonNull FiatHolder holder, int position) {
            BankCardBean item = getItem(position);
            boolean isSelected = mSelItem != null && mSelItem.equals(item);
            holder.llAddNewpay.setVisibility(View.GONE);
            holder.tvTitle.setVisibility(View.VISIBLE);
            switch (item.getType()) {//1微信2支付宝3银行卡
                case 1:
                    holder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(
                            item.getType() == BANK_TYPE_WRITE ? null
                                    : ResourceUtil.getDrawable(BankType.getBankTypeByValue(item.getType()).getSelectPaymentMethodLogo()), null,
                            isSelected ? ResourceUtil.getDrawable(R.mipmap.icon_deal_payment_method_checked) : null,
                            null);
                    holder.tvTitle.setText(R.string.weixin);
                    break;
                case 2:
                    holder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(
                            item.getType() == BANK_TYPE_WRITE ? null
                                    : ResourceUtil.getDrawable(BankType.getBankTypeByValue(item.getType()).getSelectPaymentMethodLogo()), null,
                            isSelected ? ResourceUtil.getDrawable(R.mipmap.icon_deal_payment_method_checked) : null,
                            null);
                    holder.tvTitle.setText(R.string.alipay);
                    break;
                case 3:
                    holder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(
                            item.getType() == BANK_TYPE_WRITE ? null
                                    : ResourceUtil.getDrawable(BankType.getBankTypeByValue(item.getType()).getSelectPaymentMethodLogo()), null,
                            isSelected ? ResourceUtil.getDrawable(R.mipmap.icon_deal_payment_method_checked) : null,
                            null);
                    String payNo = StringUtil.emptyIfNull(item.getPayNo());
                    holder.tvTitle.setText(String.format("%s(%s)", StringUtil.emptyIfNull(item.getBankName()), payNo.length() > 4
                            ? payNo.substring(payNo.length() - 4) : payNo));
                    break;
                default:
                    holder.llAddNewpay.setVisibility(View.VISIBLE);
                    holder.tvTitle.setVisibility(View.GONE);
//                    holder.tvTitle.setText(StringUtil.emptyIfNull(item.getBankName()));
                    holder.tvAddNew_Pay.setText(StringUtil.emptyIfNull(item.getBankName()));
                    break;
            }
        }


        @Override
        public FiatHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new FiatHolder(mInflater.inflate(R.layout.item_select_order_payment_mothod, parent, false));
        }

        void setSelPosition(BankCardBean selItem) {
            mSelItem = selItem;
            notifyDataSetChanged();
        }
    }

    static class FiatHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_add_new_pay)
        TextView tvAddNew_Pay;
        @BindView(R.id.ll_add_new_pay)
        LinearLayout llAddNewpay;

        FiatHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public OrderPaymentMethodPopupWindow setOnSelPayMethodListener(OrderPaymentMethodPopupWindow.OnSelPayMethodListener onSelPayMethodListener) {
        mOnSelPayMethodListener = onSelPayMethodListener;
        return this;
    }


    public interface OnSelPayMethodListener {
        void onSelPayMethod(BankCardBean bankCardBean);
    }


}
